package com.universe.utils;

public class Vector {
	
	private int dimension;
	private double[] vec;
	
	public Vector(double... vals){
		dimension = vals.length;
		
		vec = new double[dimension];
		for(int i=0; i<dimension; i++){
			vec[i] = vals[i];
		}
	}
	
	public double get(int i){
		return vec[i];
	}
	
	public int getDimension(){
		return dimension;
	}
	
	public Vector plus(Vector v){
		if(this.dimension != v.dimension){
			throw new RuntimeException("Vectors differ in dimensions");
		}
		double[] vals = new double[this.dimension];
		for(int i=0; i<this.dimension; i++){
			vals[i] = this.vec[i] + v.vec[i];
		}
		return new Vector(vals);
	}
	
	public Vector minus(Vector v){
		if(this.dimension != v.dimension){
			throw new RuntimeException("Vectors differ in dimensions");
		}
		double[] vals = new double[this.dimension];
		for(int i=0; i<this.dimension; i++){
			vals[i] = this.vec[i] - v.vec[i];
		}
		return new Vector(vals);
	}
	
	public Vector times(double d){
		double vals[] = new double[this.dimension];
		for(int i=0; i < vals.length; i++){
			vals[i] = this.vec[i] * d;
		}
		return new Vector(vals);
	}
	
	public double times(Vector v){
		if(this.dimension != v.dimension){
			throw new RuntimeException("Vectors differ in dimensions");
		}
		double rez = 0;
		for(int i=0; i<this.dimension; i++){
			rez += this.vec[i] * v.vec[i];
		}
		return rez;
	}
	
	public double magnitude(){
		return Math.sqrt(this.times(this));
	}
	
	public String toString(){
		StringBuilder rez = new StringBuilder();
		rez.append("{ ");
		for(int i=0; i < vec.length; i++){
			rez.append(vec[i]);
			if(i==vec.length-1){
				break;
			}
			rez.append(", ");
		}
		rez.append(" }");
		return rez.toString();
	}
	
	public boolean equals(Object o){
		if(o instanceof Vector){
			return this.equals((Vector)o);
		}
		return this == o;
	}
	
	public boolean equals(Vector v){
		if(v.dimension != this.dimension){
			return false;
		}
		for(int i=0; i<dimension; i++){
			if(this.vec[i] != v.vec[i]){
				return false;
			}
		}
		return true;
	}
}
